<!--
 * @Description: 定时任务
 * @Autor: Zhu_liangyu
 * @Date: 2020-05-20 20:07:14
--> 
<template>
  <div>
    <page-table
      rowKey="cronTaskId"
      ref="cronTaskPageTable"
      :query-api="cronTask_queryPageApi"
      :delete-api="cronTask_deletesApi"
      :columns="columns"
      :query-param="queryParam"
      @addClick="addCronTaskForm"
    >
      <template #customCronTaskName="{text,record}">
        <qc-link @click="editCronTaskForm(record)">{{text}}</qc-link>
      </template>
      <template #customActive="{text}">
        <a-badge :status="text?'success':'default'" :text="text?'激活':'关闭'"></a-badge>
      </template>
      <template #customOperation="{record}">
        <a-button
          size="small"
          :type="record.active?'danger':'primary'"
          @click="changeActive(record)"
        >{{record.active?'停止':'激活'}}</a-button>
        <a-button
          size="small"
          style="margin-left:10px;"
          type="primary"
          @click="runOnceJob(record)"
        >执行</a-button>
      </template>
    </page-table>
    <a-modal
      title="定时任务详情"
      :visible="visible"
      @cancel="()=>{visible=false;currentCronTaskId=null;this.reloadTable()}"
      :footer="null"
    >
      <cron-task-form :cron-task-id="currentCronTaskId" v-if="visible"></cron-task-form>
    </a-modal>
  </div>
</template>
<script>
import {
  cronTask_queryPageApi,
  cronTask_deletesApi,
  changeActive,
  runOnceJob,
} from "@/api/cms/cronTask";
import CronTaskForm from "@/views/cms/cronTask/form";
import { cronTaskTableColumns } from "@/profile/cms/cronTask";
//搜索数据
export default {
  components: { CronTaskForm },
  data() {
    return {
      columns: cronTaskTableColumns,
      queryParam: {},
      cronTask_queryPageApi,
      cronTask_deletesApi,
      visible: false,
      currentCronTaskId: null,
    };
  },
  methods: {
    reloadTable() {
      this.$refs.cronTaskPageTable.reload();
    },
    addCronTaskForm() {
      this.visible = true;
    },
    editCronTaskForm(record) {
      this.currentCronTaskId = record.cronTaskId;
      this.visible = true;
    },
    changeActive(record) {
      record.active = !record.active;
      changeActive({
        cronTaskId: record.cronTaskId,
        active: record.active,
      }).then((response) => {
        let { data } = response.data;
        this.$message.success("状态修改成功!");
        this.reloadTable();
      });
    },
    runOnceJob(record) {
      runOnceJob({ cronTaskId: record.cronTaskId }).then((response) => {
        let { data } = response.data;
        this.$message.success("任务执行成功!");
        this.reloadTable();
      });
    },
  },
};
</script>